/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

/**
 * Defines RTCView "derived" from RTCBaseView.
 * Assigns 'this.view' to OVRUI.UIView(guiSys),
 * so basically this is a view with three.js support.
 * @class RCTView
 * @extends RCTBaseView
 * @flow
 */

import RCTBaseView from './BaseView';
import merge from '../Utils/merge';
import * as OVRUI from 'ovrui';

import type {GuiSys} from 'ovrui';

export default class RCTView extends RCTBaseView {
  /**
   * constructor: allocates the required resources and sets defaults
   */
  constructor(guiSys: GuiSys) {
    super();
    this.view = new OVRUI.UIView(guiSys);

    Object.defineProperty(
      this.props,
      'pointerEvents',
      ({
        set: value => {
          this.view.setPointerEvents(value);
        },
      }: Object)
    );
    Object.defineProperty(
      this.props,
      'hitSlop',
      ({
        set: value => {
          if (typeof value === 'number') {
            this.view.setHitSlop(value, value, value, value);
          } else {
            this.view.setHitSlop(value.left, value.top, value.right, value.bottom);
          }
        },
      }: Object)
    );
    Object.defineProperty(
      this.props,
      'cursorVisibilitySlop',
      ({
        set: value => {
          if (typeof value === 'number') {
            this.view.setCursorVisibilitySlop(value, value, value, value);
          } else {
            this.view.setCursorVisibilitySlop(value.left, value.top, value.right, value.bottom);
          }
        },
      }: Object)
    );
    Object.defineProperty(
      this.props,
      'billboarding',
      ({
        set: value => {
          this.view.setBillboarding(value);
        },
      }: Object)
    );
  }

  /**
   * Customised present layout so that the border settings can be updated
   */
  presentLayout() {
    super.presentLayout();
  }

  /**
   * Describes the properies representable by this view type and merges
   * with super type
   */
  static describe(): any {
    return merge(super.describe(), {
      NativeProps: {
        pointerEvents: 'string',
        hitSlop: 'number',
        cursorVisibilitySlop: 'number',
        billboarding: 'string',
      },
    });
  }
}
